package com.ricky.game.scene;

import com.ricky.config.scene.SceneConfig;
import com.ricky.core.net.msg.AMF3Context;
import com.ricky.core.quad.QuadTree;
import com.ricky.game.Actor;
import com.ricky.game.Hero;
import com.ricky.game.MsgComressionHelper;
import com.ricky.game.MsgHelper;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
/**
 * 场景
 * @author rik
 *
 */
public class SceneController  extends SceneBase{
	
	protected static Logger log = Logger.getLogger(SceneController.class);
	
	private Boolean initialize=false;

	/**
	 * 场景控制器是否初始化
	 * @return the initialize
	 */
	public Boolean getInitialize() {
		return initialize;
	}
	
	/**
	 * 装载配置数据
	 * @param scf
	 */
	public void init(SceneConfig scf)
	{
		super.init();
		if(sceneData!=null)
		{
			sceneData=scf;
			int maxCol=(int) (sceneData.getMap().width/sceneData.getMap().titleWidth);//取最小值
			quadTree.buildTreeBySingleArray(sceneData.getMap().data, maxCol);
		}
	}
	

	@Override
	public void removeFromWaitingRoom(Hero hero) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 是否能够进入场景
	 * @param hero
	 * @param sb
	 * @return
	 */
	public Boolean prevEnter(Hero hero,int x,int y,StringBuilder sb)
	{
	
		return true;
	}

	/**
	 * 添加到等待室
	 * @param hero
	 * @param x
	 * @param y
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void joinWaitingRoom(Hero hero,int x,int y,int z)
	{
		
		Map<String,Object> data=new HashMap();
		waiting.put(hero.getName(), hero);//放入加载列表
		if(x==0 || y==0)
		{
			int[] xy=sceneData.getMap().getBorn(0);
			hero.getGraphic().setX(xy[0]);
			hero.getGraphic().setY(xy[1]);
			hero.getGraphic().setZ(xy[1]);
		}else
		{
			hero.getGraphic().setX(x);
			hero.getGraphic().setY(y);
			hero.getGraphic().setZ(z);
		}
		
		data.put("cmd", "l.loadMap");
		data.put("mapURL", sceneData.getMapURL());
		data.put("sceneID", sceneData.getSceneID());
		data.put("hero", hero);
		MsgHelper.sendMsg(hero.getName(), data);
	}
	
	/**
	 * 同步位置
	 * @param hero
	 */
	public void synchrostep(Hero hero)
	{
		
	}
	
	/**
	 * 场景主循环
	 */
	public void update()
	{
		
	}
	
	/**
	 * 地图加载完成进入场景
	 * @param hero
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public void enter(Hero hero)
	{
		String key=hero.getName();
		waiting.remove(key);
		online.put(key, hero);
		hero.setCurrentScene(this);
//		Map pos=MsgComressionHelper.heroPostionMsg(hero);

		Map<String,Object> data=new HashMap<String,Object>();
		data.put("cmd", "l.enter");
		data.put("sceneID", sceneData.getSceneID());
		data.put("pos", hero.getGraphic());
		data.put("x", hero.getGraphic().getX());
		data.put("y", hero.getGraphic().getY());
		data.put("z", hero.getGraphic().getZ());
		
		MsgHelper.sendMsg(hero.getRequest(), data);
		
		//告诉其他玩家有人加入进来了
		Map<String,Object> data1=new HashMap<String,Object>();
		data1.put("cmd", "l.addHero");
		data1.put("hero", hero);
		broadcast(hero,data1);
		
		//告诉自己周围有哪些玩家
		Collection<Hero> actors= this.online.values(); //quadTree.getAroundLeaf(hero.getX(), hero.getY(), 6);
		for (Actor act:actors)
		{
			if(act!=hero)
			{
				Map<String,Object> data2=new HashMap<String,Object>();
				data2.put("cmd", "l.addHero");
				data2.put("hero", act);
				AMF3Context amf=new AMF3Context();
				amf.setContent(data2);
				if(hero.getRequest()!=null )//&& act!=hero
					MsgHelper.sendMsg(hero.getRequest(), amf);
			}
		}
	}


	/**
	 * 移出英雄
	 * @param hero
	 * @return
	 */
	public void remove(Hero hero)
	{
		Map<String,Object> data1=new HashMap<String,Object>();
		data1.put("cmd", "l.removeHero");
		data1.put("hero", hero);
		broadcast(hero,data1);
		hero.setCurrentScene(null);
	}
	
	//******************* privte ***************************
	private SceneConfig sceneData;

	private QuadTree<Actor> quadTree=new QuadTree<Actor>() ;
//	public Actor[][] players; 


}
